/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package VietDQ;

import Entity.Account;
import Entity.Category;
import Entity.Event;
import Entity.Faq;
import Entity.Payment;
import NamNH.NamNHSessionBeanLocal;
import NamNH.UserPayDTO;
import PhuocPH.EventCommentsDTO;
import PhuocPH.PhuocPHSessionBeanLocal;
import java.io.IOException;
import java.io.PrintWriter;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *

 */
public class VietDQServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String action = request.getParameter("action");
            HttpSession session = request.getSession(true);
            Context ctx = new InitialContext();
            VietDQSessionBeanLocal VLocal = (VietDQSessionBeanLocal) ctx.lookup("VietDQSessionBeanLocalJNDI");
             PhuocPHSessionBeanLocal PLocal = (PhuocPHSessionBeanLocal) ctx.lookup("PhuocPHSessionBeanLocalJNDI");
             NamNHSessionBeanLocal NLocal = (NamNHSessionBeanLocal) ctx.lookup("NamNHSessionBeanLocalJNDI");
            if(action.equals("AdviewFAQ"))
            {
                Faq[] objFa = VLocal.getFAQ();
                session.setAttribute("ADVIEWFAQ", objFa);
                request.getRequestDispatcher("ADManagerFAQ.jsp").forward(request, response);
            }
            else if(action.equals("ViewFAQ"))
            {
                Faq[] objFa = VLocal.getFAQ();
                session.setAttribute("VIEWFAQ", objFa);
                request.getRequestDispatcher("Faq.jsp").forward(request, response);
            }
            else if(action.equals("Active"))
            {
                int id = Integer.parseInt(request.getParameter("Ac"));
                if(VLocal.activeFAQ(id))
                {
                    request.getRequestDispatcher("ADManagerFAQ.jsp").forward(request, response);
                }
            }
            else if(action.equals("unActive"))
            {
                int id = Integer.parseInt(request.getParameter("Uac"));
                if(VLocal.unActiveFAQ(id))
                {
                    request.getRequestDispatcher("ADManagerFAQ.jsp").forward(request, response);
                }
            }
            else if(action.equals("Create"))
            {
                String question = request.getParameter("txtQues");
                String answer = request.getParameter("txtAns");
                if(VLocal.AddNewFAQ(question, answer))
                {
                    session.setAttribute("mess", "Create new FAQ Successful !!!");
                   request.getRequestDispatcher("ErrorAdmin.jsp").forward(request, response);
                }
            }
             else if(action.equals("Save"))
            {
                int id = Integer.parseInt(request.getParameter("txtid"));
                String question = request.getParameter("txtQues1");
                String answer = request.getParameter("txtAns1");
                if(VLocal.UpdateFAQ(id, question, answer))
                {
                    session.setAttribute("mess", "Update FAQ Successful !!!");
                   request.getRequestDispatcher("ErrorAdmin.jsp").forward(request, response);
                }
            }
             else if(action.equals("UserdetailFAQ"))
            {
                int id = Integer.parseInt(request.getParameter("udtid"));
                Faq objFa = VLocal.findFAQByPrimarykey(id);
                session.setAttribute("UDETAILFAQ", objFa);
                request.getRequestDispatcher("UsDetailFAQ.jsp").forward(request, response);
            }
             else if(action.equals("Edit"))
            {
                int id = Integer.parseInt(request.getParameter("dtid"));
                Faq objFa = VLocal.findFAQByPrimarykey(id);
                session.setAttribute("editFAQ", objFa);
                request.getRequestDispatcher("AdUpdateFAQ.jsp").forward(request, response);
            }
            else if(action.equals("detailFAQ"))
            {
                int id = Integer.parseInt(request.getParameter("dtid"));
                Faq objFa = VLocal.findFAQByPrimarykey(id);
                session.setAttribute("DETAILFAQ", objFa);
                request.getRequestDispatcher("AdDetailFAQ.jsp").forward(request, response);
            }else if(action.equals("GetAllEvents"))
                {
                   Event[] resultEv = VLocal.GetAllEvents();
                   if(resultEv.length < 0)
                   {
                         request.getRequestDispatcher("AddNewEvents.jsp").forward(request, response);
                   }else
                   {
                         session.setAttribute("Event_List", resultEv);
                         response.sendRedirect("ManageEvents.jsp");

                   }
            }else if(action.equals("btn_AddEvent"))
            {
                    Category[] objCate = PLocal.GetCategory();
                    request.getSession().setAttribute("Cate_combo", objCate);
                    request.getRequestDispatcher("AddNewEvents.jsp").forward(request, response);
            }
             else if(action.equals("UserGetAllEvents"))
                {
                   Event[] resultEv = VLocal.GetAllEvents();
                   session.setAttribute("UsEvent_List", resultEv);
                   response.sendRedirect("Event.jsp");
            }
            else if(action.equals("USDetailEV"))
            {
                int id = Integer.parseInt(request.getParameter("txtid"));
                Event objE = VLocal.findEventByPK(id);
                EventCommentsDTO listComOfEv = PLocal.CommentsOfEvent(id);
                session.setAttribute("COMMENTEVENT", listComOfEv);
                session.setAttribute("DETAILEVENT", objE);
                
                request.getRequestDispatcher("USViewDetailEV.jsp").forward(request, response);
            }
            else if(action.equals("GoEvDetail"))
            {
                Event objE = VLocal.findEventByPK(Integer.parseInt(request.getParameter("EvId")));
                session.setAttribute("ADEV", objE);
                request.getRequestDispatcher("ADDetailEvent.jsp").forward(request, response);
            }
            else if(action.equals("GoListDoc"))
            {
                EventDocumentDTO objDTO = (EventDocumentDTO)VLocal.findDocumentByEvent(Integer.parseInt(request.getParameter("EventID")));
                session.setAttribute("ListDocument", objDTO);
                request.getRequestDispatcher("ADListDocOfEvent.jsp").forward(request, response);
            }else if(action.equals("GoVisit"))
            {
                int idEv = Integer.parseInt(request.getParameter("EventID"));
                EventDocumentDTO objDTO = (EventDocumentDTO)VLocal.findDocumentByEvent(idEv);
                Event objE = (Event)VLocal.findEventByPK(idEv);
                session.setAttribute("ListDocument", objDTO);
                session.setAttribute("winner", objE);
                request.getRequestDispatcher("ADListDocOfEventOK.jsp").forward(request, response);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
